package com.nondo.rdp.stock.logic;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.nondo.rdp.stock.common.EastMoneyDataSource;
import com.nondo.rdp.stock.common.HttpUtils;
import com.nondo.rdp.stock.common.WallstreetDataSource;

import org.springframework.stereotype.Component;


@Component
public class MarketLogic {
    /**
     * 获取单支股票信息
     *
     * @param prodCode string 股票代码
     * @return
     */
    public JSONObject getOne(String prodCode) {
        String realData = HttpUtils.sendGet(WallstreetDataSource.URL_REAL, WallstreetDataSource.URL_REAL_PARAM + prodCode);
        return getUsedObjectData(realData, "snapshot");
    }

    /**
     * 获取股票的分时图信息
     *
     * @param prodCode
     * @return
     */
    public JSONObject getTrend(String prodCode) {
        String realData = HttpUtils.sendGet(WallstreetDataSource.URL_TREND, WallstreetDataSource.URL_TREND_PARAM + prodCode);
        return getUsedObjectData(realData, "trend");
    }

    /**
     * 获取股票的K线图信息
     *
     * @param prodCode     string 股票代码
     * @param candlePeriod int K线类型,6=日K,7=周K,8=月K,1=1分钟,2=5分钟,3=15分钟,4=30分钟,5=60分钟
     * @param dataCount    int 请求数据多少
     * @return
     */
    public JSONObject getKline(String prodCode, int candlePeriod, int dataCount, String adjustPriceType) {
        String realData = HttpUtils.sendGet(WallstreetDataSource.URL_KLINE, WallstreetDataSource.getUrlKParam(prodCode, candlePeriod, dataCount, adjustPriceType));
        return getUsedObjectData(realData, "candle");
    }

    /**
     * 通过关键字搜索股票
     *
     * @param key string 搜索关键字
     * @return
     */
    public JSONArray search(String key) {
        String realData = HttpUtils.sendGet(WallstreetDataSource.URL_SEARCH, WallstreetDataSource.URL_SEARCH_PARAM + key);
        return getUsedArrayData(realData, "finances");
    }

    /**
     * 获取三个指标
     *
     * @return
     */
    public JSONObject three() {
        String realData = HttpUtils.sendGet(WallstreetDataSource.URL_REAL, WallstreetDataSource.URL_REAL_THREE_PARAM);
        return getUsedObjectData(realData, "snapshot");
    }

    /**
     * 沪深首页涨列表
     *
     * @return
     */
    public JSONObject upList() {
        String realData = HttpUtils.sendGet(WallstreetDataSource.URL_UP_DOWN, WallstreetDataSource.URL_UD_HOME_UP_PARAM);
        return getUsedObjectData(realData, "sort");
    }

    /**
     * 沪深首页跌列表
     *
     * @return
     */
    public JSONObject downList() {
        String realData = HttpUtils.sendGet(WallstreetDataSource.URL_UP_DOWN, WallstreetDataSource.URL_UD_HOME_DOWN_PARAM);
        return getUsedObjectData(realData, "sort");
    }

    /**
     * 获取带有分页的涨跌列表
     *
     * @param startPos long 选择数量
     * @param sortType long 涨或者跌,1=涨,0=跌
     * @return
     */
    public JSONObject upDownList(long startPos, long sortType) {
        String realData = HttpUtils.sendGet(WallstreetDataSource.URL_UP_DOWN, WallstreetDataSource.URL_UD_PARAM + "&sort_type=" + sortType + "&start_pos=" + startPos);
        return getUsedObjectData(realData, "sort");
    }


    /**
     * 处理数据,返回JSONObject对象
     *
     * @param data
     * @param field
     * @return
     */
    private JSONObject getUsedObjectData(String data, String field) {
        JSONObject realDataJson = JSONObject.parseObject(data);
        JSONObject realDataJsonData = realDataJson.getJSONObject("data");
        JSONObject realDataJsonSort = realDataJsonData.getJSONObject(field);
        return realDataJsonSort;
    }

    /**
     * 处理数据,返回JSONArray列表对象
     *
     * @param data
     * @param field
     * @return
     */
    private JSONArray getUsedArrayData(String data, String field) {
        JSONObject realDataJson = JSONObject.parseObject(data);
        JSONObject realDataJsonData = realDataJson.getJSONObject("data");
        JSONArray realDataJsonSort = realDataJsonData.getJSONArray(field);
        return realDataJsonSort;
    }

    /**
     * 获取带有分页的涨跌概念列表
     *
     * @param startPos int 选择数量
     * @param sortType int 涨或者跌,1=涨,-1=跌
     * @return
     */
	public JSONArray upDownConceptList(int startPos, int sortType) {
		String realData = HttpUtils.sendGet(EastMoneyDataSource.URL_UP_DOWN_CONCEPT_LIST, EastMoneyDataSource.URL_UP_DOWN_CONCEPT_LIST_PARAM + "&sr=" + sortType + "&ps=" + startPos);
		realData = realData.replaceAll("\\(|\\)", "").replaceAll("\\(|\\)", "");
		System.out.println(realData);
		JSONArray jsonArray = JSONArray.parseArray(realData);
		return jsonArray;
	}
}